package com.rightsidetech.scheduler.task;

import com.rightsidetech.commom.push.AgentPushUtil;
import com.rightsidetech.commom.push.Device;
import com.rightsidetech.commom.push.PushModel;
import com.rightsidetech.commom.push.StorePushUtil;
import com.rightsidetech.system.entity.SysAgentLoginHistory;
import com.rightsidetech.system.entity.store.SysStoreLoginHistory;
import com.rightsidetech.system.service.SysAgentInfoService;
import com.rightsidetech.system.service.SysStoreInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


/**
 * @Package: com.rightsidetech.http.push
 * @ClassName: MassageTask
 * @Author: yinj
 * @CreateTime: 2021-12-27 10:18:53
 * @Description: 推送消息异步任务
 */
@Component
@Slf4j
public class ScMassageTask {

    @Resource
    private SysAgentInfoService sysAgentInfoService;

    @Resource
    private SysStoreInfoService sysStoreInfoService;


    /**
     * --------------门店推送--------------------------------
     * 2,上门服务订单  4,电池检测审核已处理,请注意查看
     *
     * @param storeId
     * @param text
     * @param type
     */
    @Async
    public void storePush(Long storeId, String text, Byte type) {
        SysStoreLoginHistory loginHistory = sysStoreInfoService.selectLoginHistoryByStoreId(storeId);
        if (!ObjectUtils.isEmpty(loginHistory)) {
            PushModel model = new PushModel();
            model.setTitle("超威云商-客户端");
            model.setText(text);
            model.setUnReadNum(1);
            model.setStringId(loginHistory.getLoginInfoId().toString());
            model.setPushType(type);
            Device device = new Device();
            device.setDeviceId(loginHistory.getDeviceId());
            device.setDeviceType(loginHistory.getDeviceType().intValue());
            device.setRiderId(loginHistory.getLoginInfoId().toString());
            try {
                StorePushUtil.pushToSingle(device, model);
                log.info(text + "推送成功");
            } catch (Exception e) {
                log.error(text + "推送失败", e);
            }
        }
    }

    /**
     * --------------代理商推送--------------------------------
     * 3,骑行活动申请已通过/已拒绝
     * 7,门店库存到期
     * @param agentLoginId
     * @param text
     * @param type
     */
    @Async
    public void agentPush(Long agentLoginId, String text, Byte type,Long StoreId) {
        SysAgentLoginHistory loginHistory = sysAgentInfoService.selectHistory(agentLoginId);
        if ((!ObjectUtils.isEmpty(loginHistory)
                && !StringUtils.isEmpty(loginHistory.getDeviceId())
                && !StringUtils.isEmpty(loginHistory.getDeviceTag())
                && StringUtils.isEmpty(loginHistory.getLogoutTime()))) {
            PushModel model = new PushModel();
            model.setTitle("超威云商-管理端");
            model.setText(text);
            model.setUnReadNum(1);
            model.setStringId(loginHistory.getLoginInfoId().toString());
            model.setStoreId(StoreId.toString());
            model.setPushType(type);
            Device device = new Device();
            device.setDeviceId(loginHistory.getDeviceId());
            device.setDeviceType(loginHistory.getDeviceTag().intValue());
            device.setRiderId(loginHistory.getLoginInfoId().toString());
            try {
                AgentPushUtil.pushToSingle(device, model);
                log.info(text + "推送成功");
            } catch (Exception e) {
                log.error(text + "推送失败", e);
            }
        }
    }

}
